package hiep.examandroidbasic;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class SinhVienDataSource {
	private SQLiteDatabase database;
	private DatabaseHelper dbHelper;
	private String[] allColumns = { DatabaseHelper.ID_SV,
			DatabaseHelper.Name_SV, DatabaseHelper.Age_SV,
			DatabaseHelper.Country_SV };

	public SinhVienDataSource(Context context) {
		dbHelper = new DatabaseHelper(context);
	}

	public void open() throws SQLException {
		database = dbHelper.getWritableDatabase();
	}

	public void close() {
		dbHelper.close();
	}

	public SinhVienEntity createSinhVien(String name, long age, String country) {
		ContentValues values = new ContentValues();
		values.put(DatabaseHelper.Name_SV, name);
		values.put(DatabaseHelper.Age_SV, age);
		values.put(DatabaseHelper.Country_SV, country);
		long insertId = database.insert(DatabaseHelper.TABLE_SinhVien, null,
				values);
		Cursor cursor = database.query(DatabaseHelper.TABLE_SinhVien,
				allColumns, DatabaseHelper.ID_SV + " = " + insertId, null,
				null, null, null);
		cursor.moveToFirst();
		SinhVienEntity newComment = cursorToComment(cursor);
		cursor.close();
		return newComment;
	}

	public void updateSinhVien(SinhVienEntity SinhVienEntity) {
		ContentValues values = new ContentValues();
		values.put(DatabaseHelper.Name_SV, SinhVienEntity.getName());
		values.put(DatabaseHelper.Age_SV, SinhVienEntity.getAge());
		values.put(DatabaseHelper.Country_SV, SinhVienEntity.getCountry());
		String whereClause = DatabaseHelper.ID_SV + " = ?";
		String[] whereArgs = new String[] { String.valueOf(SinhVienEntity
				.getId()) };
		database.update(DatabaseHelper.TABLE_SinhVien, values, whereClause,
				whereArgs);

		int id = SinhVienEntity.getId();
		Log.e("CommentsDataSource", "Comment update with id: " + id);
	}

	public void deleteSinhVien(SinhVienEntity sinhvienEntity) {
		int id = sinhvienEntity.getId();
		Log.e("CommentsDataSource", "Comment deleted with id: " + id);
		database.delete(DatabaseHelper.TABLE_SinhVien, DatabaseHelper.ID_SV
				+ " = " + id, null);
	}

	public List<SinhVienEntity> getAllSinhVien() {
		List<SinhVienEntity> sinhvienEntities = new ArrayList<SinhVienEntity>();

		Cursor cursor = database.query(DatabaseHelper.TABLE_SinhVien,
				allColumns, null, null, null, null, null);

		cursor.moveToFirst();
		while (!cursor.isAfterLast()) {
			SinhVienEntity sinhvienEntity = cursorToComment(cursor);
			sinhvienEntities.add(sinhvienEntity);
			cursor.moveToNext();
		}
		cursor.close();
		return sinhvienEntities;
	}

	public List<SinhVienEntity> searchSinhVienByName(String name) {
		List<SinhVienEntity> sinhvienEntities = new ArrayList<SinhVienEntity>();

		Cursor cursor = database.query(DatabaseHelper.TABLE_SinhVien,
				allColumns, "Name LIKE '%" + name + "%'", null, null, null,
				null);

		cursor.moveToFirst();
		while (!cursor.isAfterLast()) {
			SinhVienEntity sinhvienEntity = cursorToComment(cursor);
			sinhvienEntities.add(sinhvienEntity);
			cursor.moveToNext();
		}
		cursor.close();
		return sinhvienEntities;
	}

	public List<SinhVienEntity> searchSinhVienByAge(String age) {
		List<SinhVienEntity> sinhvienEntities = new ArrayList<SinhVienEntity>();

		Cursor cursor = database.query(DatabaseHelper.TABLE_SinhVien,
				allColumns, "Age = " + age, null, null, null, null);

		cursor.moveToFirst();
		while (!cursor.isAfterLast()) {
			SinhVienEntity sinhvienEntity = cursorToComment(cursor);
			sinhvienEntities.add(sinhvienEntity);
			cursor.moveToNext();
		}
		cursor.close();
		return sinhvienEntities;
	}

	public List<SinhVienEntity> searchSinhVienByCountry(String country) {
		List<SinhVienEntity> sinhvienEntities = new ArrayList<SinhVienEntity>();

		Cursor cursor = database.query(DatabaseHelper.TABLE_SinhVien,
				allColumns, "Country LIKE '%" + country + "%'", null, null,
				null, null);

		cursor.moveToFirst();
		while (!cursor.isAfterLast()) {
			SinhVienEntity sinhvienEntity = cursorToComment(cursor);
			sinhvienEntities.add(sinhvienEntity);
			cursor.moveToNext();
		}
		cursor.close();
		return sinhvienEntities;
	}

	private SinhVienEntity cursorToComment(Cursor cursor) {
		SinhVienEntity SinhVienEntity = new SinhVienEntity();
		SinhVienEntity.setId(cursor.getInt(cursor
				.getColumnIndex(DatabaseHelper.ID_SV)));
		SinhVienEntity.setName(cursor.getString(cursor
				.getColumnIndex(DatabaseHelper.Name_SV)));
		SinhVienEntity.setAge(cursor.getInt(cursor
				.getColumnIndex(DatabaseHelper.Age_SV)));
		SinhVienEntity.setCountry(cursor.getString(cursor
				.getColumnIndex(DatabaseHelper.Country_SV)));
		return SinhVienEntity;
	}
}
